package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbDepartUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbDepartUserDao extends JpaRepository<TbDepartUser, Integer> {

	TbDepartUser getByDepartIdAndUserId(Integer departId, Integer userId);

	@Query("select d.id from TbDepartUser d where d.userId=:userId")
	List<Integer> findAllIdByUserId(@Param("userId") Integer userId);

	@Query("select d.id from TbDepartUser d where d.userId=:userId and d.id not in(:idList)")
	List<Integer> findAllIdByUserIdAndIdNotIn(@Param("userId") Integer userId, @Param("idList") List<Integer> idList);

	@Query("select d.id from TbDepartUser d where d.departId=:departId")
	List<Integer> findAllIdByDepartId(@Param("departId") Integer departId);

	@Query("select d.id from TbDepartUser d where d.departId=:departId and d.id not in(:idList)")
	List<Integer> findAllIdByDepartIdAndIdNotIn(@Param("departId") Integer departId, @Param("idList") List<Integer> idList);


	@Modifying
	@Query("delete from TbDepartUser d where d.id in(:idList)")
	void deleteByIdIn(@Param("idList") List<Integer> idList);


	@Query("select d.userId from TbDepartUser d where d.departId=:departId")
	List<Integer> findAllUserIdByDepartId(@Param("departId") Integer departId);


	@Query("select d.departId from TbDepartUser d where d.userId=:userId")
	List<Integer> findAllDepartIdByUserId(@Param("userId") Integer userId);
}
